$(function() {
    //选中地址
    $(document).on('click', '.addr-card', function() {
        $(this).addClass('active').siblings().removeClass('active');
    })
    //设置默认地址
    $(document).on('click', '.addr-deafault:not(active)', function() {
        var $div = $(this).parents('.addr-card');
        var id = $div.data('id');
        var url = $('.address-list').data('url');
        $.ajax({
            url: url,
            type: 'POST',
            //dataType: 'json',
            data: {id: id},
            success: function(e) {

            }
        })
        $('.addr-deafault.active').removeClass('active');
        $(this).addClass('active');
    })
    //新建地址
    $('.create-addr').click(function() {
        $('.md-modal').addClass('md-show');
        $('.md-overlay').addClass('md-overlay-show');
    })
    //
    $('.md-close,.close-md,.md-overlay').click(function() {
        $('#form_addr')[0].reset();
        $('.md-modal').removeClass('md-show');
        $('.md-overlay').removeClass('md-overlay-show');
    })
})
function addCartAddr() {
    var obj = $('#add_newaddr');
    var url = $(obj).data('url');
    var truename = $("#truename").val();
    var phone = $("#phone").val();
    var phone2 = $("#phone2").val();
    var addr = $("#addr").val();
    $(obj).button('loading');
    $.ajax({
        url: url,
        dataType: 'html',
        type: "post",
        data: $('#form_addr').serialize(),
        success: function(e) {
            $(obj).button('reset');
            $(".addr-card").removeClass("active");
            var div = "<div data-id=" + e + " class='addr-card active'>\n\
                    <div class='inner'>\n\
                        <div class='addr-hd'>\n\
                            <span class'name'>" + truename + " 收</span>\n\
                            <i class='icon-map-marker addr-deafault' title='默认地址'></i>\n\
                        </div>\n\
                        <div title='' class='addr-bd'>\n\
                            <span class='street'>" + addr + "</span>\n\
                            <p class='phone' style='margin:5px 0'>" + phone + "</p>\n\
<p class='phone2'>" + phone2 + "</p>\n\
                        </div>\n\
                        <div class='addr-toolbar'></div>\n\
                    </div>\n\
                    <div class='corner-left'></div>\n\
                    <div class='corner-right'></div>\n\
                </div>";
            $('.address-list').prepend(div);
            $('.md-modal').removeClass('md-show');
            $('.md-overlay').removeClass('md-overlay-show');
        }
    })
}
function check_cart(obj) {
    var checked = obj.prop("checked");
    if (checked == true) {
        $("#cart_area").find("input[type=checkbox]").prop("checked", true);
    } else {
        $("#cart_area").find("input[type=checkbox]").prop("checked", false);
    }
    getCartChosePrice();
}
function check_shop(obj) {
    var checked = obj.prop("checked");
    var area = obj.parent(".c_shop").next(".order-content");
    area.find("input[type=checkbox]").prop("checked", true);
    if (checked == true) {
        area.find("input[type=checkbox]").prop("checked", true);
    } else {
        area.find("input[type=checkbox]").prop("checked", false);
    }
    checkCartCheckboxAll();
}
function checkCartCheckboxAll() {//检测所有多选框
    var len = $("#cart_area").find("input[type=checkbox]").length;
    var len2 = $("#cart_area").find("input[type=checkbox]:checked").length;

    if (len == len2 + 2) {
        $(".check_cart").prop("checked", true);
    } else {
        $(".check_cart").prop("checked", false);
    }
    getCartChosePrice();
}

function check_goods(obj) {
    var area = obj.parents(".order-content");
    var len = area.find("input[type=checkbox]").length;
    var len2 = area.find("input[type=checkbox]:checked").length;
    if (len == len2 && len2 > 0) {
        area.prev('.c_shop').find("input[type=checkbox]").prop("checked", true);
    } else {
        area.prev('.c_shop').find("input[type=checkbox]").prop("checked", false);
    }
    getCartChosePrice();
}

function cart_goods_plus(obj) {
    var num = parseInt(obj.prev("input").val());
    obj.prev("input").val(num + 1);
    var price = obj.parents(".td-amount").prev(".td-price").text();

    $.post(getUrl("Cart/addCut"), {
        id: obj.parents(".item-content").find(".check_goods").val(),
        num: (num + 1)
    }, function(data) {
        var sum = price * (num + 1);
        obj.parents(".td-amount").next(".td-sum").find("strong").text(sum.toFixed(1));
        getCartChosePrice();
    })
}
function cart_goods_minus(obj) {
    var num = parseInt(obj.next("input").val());
    if (num > 1) {
        obj.next("input").val(num - 1);
        var price = obj.parents(".td-amount").prev(".td-price").text();

        $.post(getUrl("Cart/addCut"), {
            id: obj.parents(".item-content").find(".check_goods").val(),
            num: (num - 1)
        }, function(data) {
            var sum = price * (num - 1);
            obj.parents(".td-amount").next(".td-sum").find("strong").text(sum.toFixed(1));
            getCartChosePrice();
        })
    }
}
function cart_goods_del(obj) {
    $.post(getUrl("Cart/addCut"), {
        id: obj.parents(".item-content").find(".check_goods").val(),
        num: 0
    }, function(data) {
        obj.parents(".bundle").remove();
        getCartChosePrice();
    })
}
function getCartChosePrice() {
    var total = 0;
    var cart_num = 0;
    $(".check_goods").each(function() {
        if ($(this).prop("checked") == true) {
            var p = $(this).parent(".td-chk").parent(".item-content");
            var sum = parseFloat(p.find(".td-sum").children("strong").text());
            total += sum;
            cart_num += parseInt(p.find(".text-amount").val());
        }
    })
    $(".cart_price_total").text(total.toFixed(1));
    if(total>0){
        $(".btn_check").removeClass("submit-btn-disabled");
    }else{
        $(".btn_check").addClass("submit-btn-disabled");
    }
    $("#cart_num_chose").text(cart_num);
}
function btn_cart_submit(obj){
    if(!obj.hasClass("submit-btn-disabled")){
        document.form_cart.submit();
    }
    
}